home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Misc / bgui / Examples / Source / PaletteDemo.c < prev    next >
C/C++ Source or Header  |  2000-05-09  |  8KB  |  319 lines

  1. /*
  2.  * @(#) $Header: /cvsroot/bgui/gadgets/Palette/PaletteDemo.c,v 41.11 2000/05/09 20:35:04 mlemos Exp $
  3.  *
  4.  * PaletteDemo.c
  5.  *
  6.  * (C) Copyright 1999 BGUI Developement team.
  7.  * (C) Copyright 1995-1996 Jaba Development.
  8.  * (C) Copyright 1995-1996 Jan van den Baard.
  9.  * All Rights Reserved.
  10.  *
  11.  * $Log: PaletteDemo.c,v $
  12.  * Revision 41.11  2000/05/09 20:35:04  mlemos
  13.  * Bumped to revision 41.11
  14.  *
  15.  * Revision 1.2  2000/05/09 20:00:05  mlemos
  16.  * Merged with the branch Manuel_Lemos_fixes.
  17.  *
  18.  * Revision 1.1.2.1  1999/05/31 01:42:28  mlemos
  19.  * Ian sources.
  20.  *
  21.  *
  22.  *
  23.  */
  24.  
  25. #include <exec/types.h>
  26. #include <libraries/bgui.h>
  27. #include <libraries/bgui_macros.h>
  28.  
  29. #include <clib/alib_protos.h>
  30.  
  31. #include <proto/exec.h>
  32. #include <proto/intuition.h>
  33. #include <proto/bgui.h>
  34.  
  35. /*
  36.  *    Compiler stuff.
  37.  */
  38. #ifdef _DCC
  39. #define SAVEDS __geta4
  40. #define ASM
  41. #define REG(x) __ ## x
  42. #else
  43. #define SAVEDS __saveds
  44. #define ASM __asm
  45. #define REG(x) register __ ## x
  46. #endif
  47.  
  48. /*
  49.  *    Object ID's.
  50.  */
  51. #define ID_QUIT                 1
  52. #define ID_FRAME        2
  53. #define ID_SFRAME        3
  54. #define ID_LABEL        4
  55. #define ID_SLABEL        5
  56.  
  57. /*
  58.  *    Map-lists.
  59.  */
  60. ULONG p2f[]  = { PALETTE_CurrentColor, FRM_BackPen,        TAG_END };
  61. ULONG p2fs[] = { PALETTE_CurrentColor, FRM_SelectedBackPen, TAG_END };
  62. ULONG p2l[]  = { PALETTE_CurrentColor, LAB_Pen,             TAG_END };
  63. ULONG p2ls[] = { PALETTE_CurrentColor, LAB_SelectedPen,     TAG_END };
  64.  
  65. /*
  66.  *    Library base and class base.
  67.  */
  68. struct Library *BGUIBase;
  69. Class           *myButtonClass;
  70.  
  71. /*
  72.  *    Info gadget text.
  73.  */
  74. UBYTE           *InfoTxt = ISEQ_C "As you can see the colors of the below button\n"
  75.               "are normal but when you change the colors with\n"
  76.               "the palette objects the colors of the button change.\n\n"
  77.               "You can also pickup the color and drop it onto the\n"
  78.               "button. " ISEQ_B "DragNDrop" ISEQ_N " in action.\n\n"
  79.               ISEQ_B "Run the program again an notice that you can\n" ISEQ_N 
  80.               ISEQ_B "drag and drop colors of one program\n" ISEQ_N
  81.               ISEQ_B "over the button of other program." ISEQ_N ;
  82.  
  83. /*
  84.  *    Some casting macros.
  85.  */
  86. #define GAD(x) (( struct Gadget * )x )
  87. #define BDQ(x) (( struct bmDragPoint * )x )
  88. #define BDM(x) (( struct bmDragMsg * )x )
  89.  
  90. /*
  91.  *    The button we use is a very simple subclass from the
  92.  *    BGUI buttonclass to accept only drops from the four
  93.  *    paletteclass objects in this demo or from other palette
  94.  *    class objects from another task or window when they have
  95.  *    the same ID as we use here.
  96.  *
  97.  *    SAS users remember! NOSTACKCHECK or __interrupt for class
  98.  *    dispatchers, hook routines or anything else which may get
  99.  *    called by a task other than your own.
  100.  */
  101. SAVEDS ASM ULONG myButtonDispatch( REG(a0) Class *cl, REG(a2) Object *obj, REG(a1) Msg msg )
  102. {
  103.     ULONG            rc, pen, tag;
  104.  
  105.     switch ( msg->MethodID ) {
  106.  
  107.         case    BASE_DRAGQUERY:
  108.             /*
  109.              *    We only accept drops from our paletteclass objects.
  110.              *    The test here is a bit simple but this way it does
  111.              *    allow for drops from another task. Just run this demo
  112.              *    twice and DragNDrop from one window to another.
  113.              */
  114.             if (( GAD( BDQ( msg )->bmdp_Source )->GadgetID >= ID_FRAME ) &&
  115.                 ( GAD( BDQ( msg )->bmdp_Source )->GadgetID <= ID_SLABEL ))
  116.                 rc = BQR_ACCEPT;
  117.             else
  118.                 rc = BQR_REJECT;
  119.             break;
  120.  
  121.         case    BASE_DROPPED:
  122.             /*
  123.              *    Get the pen from the object.
  124.              */
  125.             GetAttr( PALETTE_CurrentColor, BDM( msg )->bmdm_Source, &pen );
  126.  
  127.             /*
  128.              *    Let's see what has been dropped...
  129.              */
  130.             switch ( GAD( BDM( msg )->bmdm_Source )->GadgetID ) {
  131.  
  132.                 case    ID_FRAME:
  133.                     tag = FRM_BackPen;
  134.                     break;
  135.  
  136.                 case    ID_SFRAME:
  137.                     tag = FRM_SelectedBackPen;
  138.                     break;
  139.  
  140.                 case    ID_LABEL:
  141.                     tag = LAB_Pen;
  142.                     break;
  143.  
  144.                 case    ID_SLABEL:
  145.                     tag = LAB_SelectedPen;
  146.                     break;
  147.  
  148.                 default:
  149.                     tag = NULL;
  150.                     break;
  151.             }
  152.  
  153.             /*
  154.              *    Set the pen. The superclass will force
  155.              *    a refresh on the object when the drop has
  156.              *    been made.
  157.              */
  158.             if(tag)
  159.                 SetAttrs( obj, tag, pen, TAG_END );
  160.             rc = 1L;
  161.             break;
  162.  
  163.         default:
  164.             rc = DoSuperMethodA( cl, obj, msg );
  165.             break;
  166.     }
  167.     return( rc );
  168. }
  169.  
  170. /*
  171.  *    Setup our button class.
  172.  */
  173. Class *MakeMyButtonClass( void )
  174. {
  175.     Class            *cl = NULL, *super;
  176.  
  177.     /*
  178.      *    Get a pointer to our superclass.
  179.      */
  180.     if ( super = BGUI_GetClassPtr( BGUI_BUTTON_GADGET )) {
  181.         /*
  182.          *    Make our class.
  183.          */
  184.         if ( cl = MakeClass( NULL, NULL, super, 0L, 0L ))
  185.             /*
  186.              *    Setup our dispatcher.
  187.              */
  188.             cl->cl_Dispatcher.h_Entry = ( HOOKFUNC )myButtonDispatch;
  189.     }
  190.     return( cl );
  191. }
  192.  
  193. /*
  194.  *    Here we go...
  195.  */
  196. int main( int argc, char **argv )
  197. {
  198.     struct Window        *window;
  199.     Object            *WO_Window, *GO_Quit, *GO_B, *GO_Pal[ 4 ]={0,0,0,0};
  200.     ULONG             signal, rc, tmp = 0, a;
  201.     UWORD             defpens[ 4 ] = { 0, 3, 1, 1 };
  202.     BOOL             running = TRUE;
  203.  
  204.     /*
  205.      *    Open BGUI.
  206.      */
  207.     if ( BGUIBase = OpenLibrary( BGUINAME, BGUIVERSION )) {
  208.             /*
  209.              *    And our drop-button class.
  210.              */
  211.             if ( myButtonClass = MakeMyButtonClass()) {
  212.                 /*
  213.                  *    I assume a depth of three
  214.                  *    (8 colors) here for simplicity.
  215.                  */
  216.                 for ( a = 0; a < 4; a++ )
  217.                     GO_Pal[ a ] = BGUI_NewObject(BGUI_PALETTE_GADGET,
  218.                                              FRM_Type,                FRTYPE_BUTTON,
  219.                                              FRM_Recessed,            TRUE,
  220.                                              PALETTE_Depth,            3,
  221.                                              PALETTE_CurrentColor,        defpens[ a ],
  222.                                              GA_ID,                a + 2,
  223.                                              BT_DragObject,            TRUE,
  224.                                              TAG_END );
  225.                 /*
  226.                  *    Create the window object.
  227.                  */
  228.                 WO_Window = WindowObject,
  229.                     WINDOW_Title,        "PaletteClass Demo",
  230.                     WINDOW_AutoAspect,    TRUE,
  231.                     WINDOW_SmartRefresh,    TRUE,
  232.                     WINDOW_RMBTrap,         TRUE,
  233.                     WINDOW_IDCMP,        IDCMP_MOUSEMOVE,
  234.                     WINDOW_MasterGroup,
  235.                         VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
  236.                             GROUP_BackFill,         SHINE_RASTER,
  237.                             StartMember,
  238.                                 InfoFixed( NULL, InfoTxt, NULL, 10 ),
  239.                             EndMember,
  240.                             StartMember,
  241.                                 HGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
  242.                                     FRM_Type,        FRTYPE_BUTTON,
  243.                                     FRM_Recessed,        TRUE,
  244.                                     StartMember, GO_B = NewObject( myButtonClass, NULL, FRM_Type, FRTYPE_BUTTON, LAB_Label, "Palette Demo", BT_DropObject, TRUE, TAG_END ), EndMember,
  245.                                 EndObject, FixMinHeight,
  246.                             EndMember,
  247.                             StartMember,
  248.                                 HGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
  249.                                     FRM_Type,        FRTYPE_BUTTON,
  250.                                     FRM_Recessed,        TRUE,
  251.                                     StartMember,
  252.                                         VGroupObject, Spacing( 4 ),
  253.                                             LAB_Label,    "Background:",
  254.                                             LAB_Place,    PLACE_ABOVE,
  255.                                             StartMember, GO_Pal[ 0 ], EndMember,
  256.                                             StartMember, GO_Pal[ 1 ], EndMember,
  257.                                         EndObject,
  258.                                     EndMember,
  259.                                     StartMember,
  260.                                         VGroupObject, Spacing( 4 ),
  261.                                             LAB_Label,    "Label:",
  262.                                             LAB_Place,    PLACE_ABOVE,
  263.                                             StartMember, GO_Pal[ 2 ], EndMember,
  264.                                             StartMember, GO_Pal[ 3 ], EndMember,
  265.                                         EndObject,
  266.                                     EndMember,
  267.                                 EndObject,
  268.                             EndMember,
  269.                             StartMember,
  270.                                 HGroupObject,
  271.                                     VarSpace( DEFAULT_WEIGHT ),
  272.                                     StartMember, GO_Quit = KeyButton( "_Quit", ID_QUIT ), EndMember,
  273.                                     VarSpace( DEFAULT_WEIGHT ),
  274.                                 EndObject, FixMinHeight,
  275.                             EndMember,
  276.                         EndObject,
  277.                 EndObject;
  278.  
  279.                 /*
  280.                  *    Object created OK?
  281.                  */
  282.                 if ( WO_Window ) {
  283.                     tmp += GadgetKey( WO_Window, GO_Quit,  "q" );
  284.                     tmp += AddMap( GO_Pal[ 0 ], GO_B, p2f  );
  285.                     tmp += AddMap( GO_Pal[ 1 ], GO_B, p2fs );
  286.                     tmp += AddMap( GO_Pal[ 2 ], GO_B, p2l  );
  287.                     tmp += AddMap( GO_Pal[ 3 ], GO_B, p2ls );
  288.                     if ( tmp == 5 ) {
  289.                         if ( window = WindowOpen( WO_Window )) {
  290.                             GetAttr( WINDOW_SigMask, WO_Window, &signal );
  291.                             do {
  292.                                 Wait( signal );
  293.                                 while (( rc = HandleEvent( WO_Window )) != WMHI_NOMORE ) {
  294.                                     switch ( rc ) {
  295.  
  296.                                         case    WMHI_CLOSEWINDOW:
  297.                                         case    ID_QUIT:
  298.                                             running = FALSE;
  299.                                             break;
  300.                                     }
  301.                                 }
  302.                             } while ( running );
  303.                         }
  304.                     }
  305.                     DisposeObject( WO_Window );
  306.                 }
  307.                 FreeClass( myButtonClass );
  308.             }
  309.         CloseLibrary( BGUIBase );
  310.     }
  311. }
  312.  
  313. #ifdef _DCC
  314. int wbmain( struct WBStartup *wbs )
  315. {
  316.     return( main( 0, wbs ));
  317. }
  318. #endif
  319.